﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
//using Oracle.DataAccess.Client;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using trangbaocao.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;
namespace trangbaocao.Models
{
    public class Main_Sort_DeviceModel
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Tên chủng loại không được để trống!")]
        public string name { get; set; }
        public string ghi_chu { get; set; }

        public List<Main_Sort_DeviceModel> getChungloaithietbi()
        {
            List<Main_Sort_DeviceModel> main_sort_device_ = new List<Main_Sort_DeviceModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM Main_Sort_Device order by name");
            while (odr.Read())
            {
                main_sort_device_.Add(new Main_Sort_DeviceModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), ghi_chu = odr[2].ToString() });
            }
            return main_sort_device_;
        }
        public List<Main_Sort_DeviceModel> getChungloaithietbiById(int id)
        {
            List<Main_Sort_DeviceModel> main_sort_device_ = new List<Main_Sort_DeviceModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM Main_Sort_Device WHERE id=" + id);
            while (odr.Read())
            {
                main_sort_device_.Add(new Main_Sort_DeviceModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), ghi_chu = odr[2].ToString() });
            }
            return main_sort_device_;
        }
        public void addChungloaithietbi(Main_Sort_DeviceModel Main_Sort_Device)
        {
            string sql = "INSERT INTO Main_Sort_Device";
            sql += " select nvl((select max(id)+1 from Main_Sort_Device),1),'" + Main_Sort_Device.name + "','"+Main_Sort_Device.ghi_chu+"' from dual";
            General.Lib_ExecuteNonQuery(sql);
        }

        public void updateChungloaithietbi(Main_Sort_DeviceModel Main_Sort_Device)
        {
            string sql = "UPDATE Main_Sort_Device SET name='" + Main_Sort_Device.name + "', ghi_chu='"+Main_Sort_Device.ghi_chu+"' where id=" + Main_Sort_Device.id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public void deleteChungloaithietbi(int id)
        {
            string sql = "DELETE FROM Main_Sort_Device WHERE id=" + id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public int checkSubId(int id)
        {
            string sql = "SELECT * FROM Sort_Device WHERE main_sort_device_id='" + id + "'";
            return General.Lib_CheckExist(sql);
        }
        public int checkExist(Main_Sort_DeviceModel Main_Sort_Device)
        {
            string sql = "SELECT * FROM Main_Sort_Device WHERE name='" + Main_Sort_Device .name + "'";
            return General.Lib_CheckExist(sql);
        }
    }
}